﻿//-----------------------------------------------------------------------
// <copyright file="IconGraphics.cs" company="Redmist">
//     Copyright (c) Damien T Smith. All rights reserved.
// </copyright>
// <author>Damien Smith</author>
//-----------------------------------------------------------------------

namespace Redmist.UsageMonitor.IconCreation
{
    using System;
    using System.Drawing;
    using System.Drawing.Text;

    /// <summary>A graphics drawing surface for drawing icons.</summary>
    public class IconGraphics : IGraphics
    {
        /// <summary>A System.Drawing.Graphics representing the GDI+ drawing surface.</summary>
        private Graphics graphics;

        /// <summary>Initializes a new instance of the IconGraphics class.</summary>
        /// <param name="graphics">A System.Drawing.Graphics representing the GDI+ drawing surface.</param>
        public IconGraphics(Graphics graphics)
        {
            this.graphics = graphics;
        }

        /// <summary>Gets or sets the rendering mode for text associated with this System.Drawing.Graphics.</summary>
        public TextRenderingHint TextRenderingHint
        {
            get
            {
                return this.graphics.TextRenderingHint;
            }

            set
            {
                this.graphics.TextRenderingHint = value;
            }
        }

        /// <summary>Fills the interior of a rectangle.</summary>
        /// <param name="brush">A System.Drawing.Brush the determines the characteristics of the fill.</param>
        /// <param name="rectangle">A System.Drawing.Rectangle representing the rectangle to fill.</param>
        public void FillRectangle(Brush brush, Rectangle rectangle)
        {
            this.graphics.FillRectangle(brush, rectangle);
        }

        /// <summary>Releases all resources used by the IconGraphics object.</summary>
        public void Dispose()
        {
            this.Dispose(true);
        }

        /// <summary>
        /// Draws the specified text string at the specified location with the specified
        /// System.Drawing.Brush and System.Drawing.Font objects using the formatting
        /// attributes of the specified System.Drawing.StringFormat.
        /// </summary>
        /// <param name="text">A System.String representing the text to draw.</param>
        /// <param name="font">A System.Drawing.Font that defines the text format of the string.</param>
        /// <param name="brush">A System.Drawing.Brush that determines the color and texture of the drawn text.</param>
        /// <param name="layoutPoint">A System.Drawing.PointF structure that specifies the upper-left corner of the drawn text.</param>
        /// <param name="format">A System.Drawing.StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.</param>
        public void DrawString(string text, Font font, Brush brush, PointF layoutPoint, StringFormat format)
        {
            this.graphics.DrawString(text, font, brush, layoutPoint, format);
        }

        /// <summary>Dispose of (clean up and deallocate) resources used by this class.</summary>
        /// <param name="fromUser"> True if called directly or indirectly from user code.  False if called from the finalizer (i.e. from the class' destructor).</param>
        /// <remarks>
        /// When called from user code, it is safe to clean up both managed and unmanaged objects.
        /// When called from the finalizer, it is only safe to dispose of unmanaged objects.
        /// This method should expect to be called multiple times without causing an exception.
        /// </remarks>
        private void Dispose(bool fromUser)
        {
            if (fromUser)
            {
                this.graphics.Dispose();
                GC.SuppressFinalize(this);
            }
        }
    }
}
